
var MOD_MOD= {
  DEFDIR :"def/",
  DATADIR:"data/",
  NAMES:[]
};
  
  

// if data is provided check if data exists and if not,
// create one using def. Then load data into DOM.
// if data is not provided, load def into DOM.
function dos_loadxmlinit(DOM,def,data) {
  var reval="load";
  if(data) {
    if(!dos_isfile(data)) {
      reval="init";
      DOM.load(def);// CreatePrefs(PREFSFILE);
      if(DOM.parseError.errorCode != 0) {
	alert(def+" not found !!");
	return "error";
      } 
      DOM.save(data);
    }
    DOM.load(data);
  } else {
    if(!dos_isfile(def)) {
      alert(def+" not found !!");
      return "error";
    }
    DOM.load(def);// CreatePrefs(PREFSFILE);
    if(DOM.parseError.errorCode != 0) {
      alert("Corrupted or empty file "+def);
      return "error";
    }         
  }
  return reval;
}

function CT_Module(name,def,data,root) {
  if(MOD_MOD.NAMES[name]) {
    alert("Module "+name+"declared twice");
    return;
  }
  
  MOD_MOD.NAMES[name]=1;
  
  this.DOM=new ActiveXObject("Msxml2.DOMDocument");
  if(def) {
    this.deffile =MOD_MOD.DEFDIR+def;
  }
  this.ready=0;
  var reval="load";
  if(data) {
    this.datafile=MOD_MOD.DATADIR+data;
    if(!dos_isfile(this.datafile)) {
      reval="init";
      this.DOM.load(this.deffile);// CreatePrefs(PREFSFILE);
      if(this.DOM.parseError.errorCode != 0) {
	alert(this.deffile+" not found !!");
	return "error";
      } 
      this.DOM.save(this.datafile);
    }
    this.DOM.load(this.datafile);
  } else if(def) {
    if(!dos_isfile(this.deffile)) {
      alert(this.deffile+" not found !!");
      return "error";
    }
    this.DOM.load(this.deffile);// CreatePrefs(PREFSFILE);
    if(this.DOM.parseError.errorCode != 0) {
      alert("Corrupted or empty file "+this.deffile);
      return "error";
    }         
  }
  if(root) {
    this.ROOT=this.DOM.selectSingleNode(root);
  }
}

CT_Module.prototype.save=function() {
  this.DOM.save(this.datafile);
}
  
CT_Module.prototype.getDOM=function() {
    return this.DOM;
}
    
CT_Module.prototype.getRoot=function() {
  return this.ROOT;
}
      
